import Cookies from "js-cookie";

const state = {
  //登录用户信息
  //loginUser: Cookies.get("loginUser") || {},
  loginUser: localStorage.getItem("loginUser") || {},
  //好友列表信息
  //friendList: Cookies.get("friendList") || [],
  friendList: localStorage.getItem("friendList") || [],
  //会话列表
  // conversationList: Cookies.get("conversationList") || [],
  conversationList: localStorage.getItem("conversationList") || [],
  //当前会话的主体对象数据{userId 好友id,好友名称 userNick 群组id groupId,群组名称 groupNick,类型 type:['friend','group']}
  //currentSessionContent: Cookies.get("currentSessionContent") || {},
  currentSessionContent: localStorage.getItem("currentSessionContent") || {},
  //historyUpdateFlag  当这个值发生变化是代表hisTory列表需要刷新
  //historyUpdateFlag: Cookies.get("historyUpdateFlag") || false,
  historyUpdateFlag: localStorage.getItem("historyUpdateFlag") || false,
  //群组列表信息
  //groupList: Cookies.get("groupList") || [],
  groupList: localStorage.getItem("groupList") || [],

  /*
	 监听电话的数据状态，数据格式

	 {
		formId:'发起人Id'，
		formName:'发起人名称',
		formAvatar:"发起人图像"
		formGroupId:"",
		inviteId:"通话编号",
		//房间号
		roomId:"",
		callType:1-语音通话，2-视频通话
		callStatus:0待接听  1正在通话 2 挂断
	 }
	 */
  currentVoiceVideoSessionContent:
    //Cookies.get("currentVoiceVideoSessionContent") || null
    localStorage.getItem("currentVoiceVideoSessionContent") || null
};

const mutations = {
  //登录成功的操作
  LOGIN_SUCCESS: (state, loginUserData = {}) => {
    localStorage.setItem("loginUser", JSON.stringify(loginUserData));
    //Cookies.set("loginUser", JSON.stringify(loginUserData));
    //state.loginUser = Cookies.get("loginUser") || [];
    state.loginUser = localStorage.getItem("loginUser");
  },
  //重置好友列表
  RESET_FRIENDLIST: (state, dataList = []) => {
    //Cookies.set("friendList", JSON.stringify(dataList));
    // state.friendList = Cookies.get("friendList") || [];
    localStorage.setItem("friendList", JSON.stringify(dataList));
    state.friendList = localStorage.getItem("friendList") || [];
  },
  //重置会话列表数据
  RESET_CONVERSATIONLIST: (state, dataList = []) => {
    //  Cookies.set("conversationList", JSON.stringify(dataList));
    //state.conversationList = JSON.stringify(dataList);
    localStorage.setItem("conversationList", JSON.stringify(dataList));
    state.conversationList = JSON.stringify(dataList);
  },

  //设置当前会话的主体对象数据
  RESET_CURRENT_SESSION_CONTENT: (state, currentData = {}) => {
    //如果点击的主体和当前的相等 就不用重置了
    //const old = Cookies.get("currentSessionContent");
    const old = localStorage.getItem("currentSessionContent");

    if (old && (old.userId || old.groupId)) {
      if (
        (currentData.userId && old.userId === currentData.userId) ||
        (currentData.groupId && old.groupId === currentData.groupId)
      ) {
        return;
      }
    }
    //Cookies.set("currentSessionContent", JSON.stringify(currentData));
    state.currentSessionContent = Cookies.get("currentSessionContent") || [];
    localStorage.setItem("currentSessionContent", JSON.stringify(currentData));

    state.currentSessionContent =
      localStorage.getItem("currentSessionContent") || [];
  },

  //退出登录 清理数据
  LOGOUT_SUCCESS: state => {
    console.log("退出成功....LOGOUT_SUCCESS");
    state.loginUser = {};
    state.friendList = [];
    state.groupList = [];
    state.currentVoiceVideoSessionContent = null;
    state.currentSessionContent = null;
    state.conversationList = [];
    // Cookies.remove("loginUser");
    // Cookies.remove("friendList");
    // Cookies.remove("groupList");
    // Cookies.remove("conversationList");
    // Cookies.remove("currentSessionContent");
    // Cookies.remove("currentVoiceVideoSessionContent");

    localStorage.removeItem("loginUser");
    localStorage.removeItem("friendList");
    localStorage.removeItem("groupList");
    localStorage.removeItem("conversationList");
    localStorage.removeItem("currentSessionContent");
    localStorage.removeItem("currentVoiceVideoSessionContent");
  },
  //重置刷新历史数据标志
  RESET_HISTORY_UPDATE_FLAG: (state, flag) => {
    // Cookies.set("historyUpdateFlag", JSON.stringify(flag));
    // state.historyUpdateFlag = Cookies.get("historyUpdateFlag") || false;
    localStorage.setItem("historyUpdateFlag", JSON.stringify(flag));
    state.historyUpdateFlag =
      localStorage.getItem("historyUpdateFlag") || false;
  },
  //重置群组列表
  RESET_GROUP_LIST: (state, dataList = []) => {
    // Cookies.set("groupList", JSON.stringify(dataList));
    // if (Cookies.get("groupList")) {
    //   state.groupList = Cookies.get("groupList") || [];
    // } else {
    //   state.groupList = JSON.stringify(dataList);
    // }

    localStorage.setItem("groupList", JSON.stringify(dataList));
    if (localStorage.getItem("groupList")) {
      state.groupList = localStorage.getItem("groupList") || [];
    } else {
      state.groupList = JSON.stringify(dataList);
    }
  },
  //设置当前语音视频通话数据
  SET_CURRENT_VOICEVIDEO_SESSION_CONTENT: (state, sessionData) => {
    // Cookies.set("currentVoiceVideoSessionContent", JSON.stringify(sessionData));
    // state.currentVoiceVideoSessionContent =
    //   Cookies.get("currentVoiceVideoSessionContent") || null;

    localStorage.setItem(
      "currentVoiceVideoSessionContent",
      JSON.stringify(sessionData)
    );
    state.currentVoiceVideoSessionContent =
      localStorage.getItem("currentVoiceVideoSessionContent") || null;
  },

  //清空语音视频通话的缓存数据
  CLEAR_CURRENT_VOICEVIDEO_SESSION_CONTENT: state => {
    // Cookies.remove("currentVoiceVideoSessionContent");
    localStorage.removeItem("currentVoiceVideoSessionContent");
    state.currentVoiceVideoSessionContent = null;
  }
};

const actions = {
  //重新设置会话列表
  resetConversationlist({ commit }, dataList = []) {
    commit(RESET_CONVERSATIONLIST, dataList);
  },
  //重新设置好友列表
  resetFriendlist({ commit }, dataList = []) {
    commit(RESET_FRIENDLIST, dataList);
  },
  //登录成功的调用
  loginSuccess({ commit }, loginUserData = {}) {
    commit(LOGIN_SUCCESS, loginUserData);
  },
  //设置当前会话的主体
  resetCurrentSessionContent({ commit }, currentData = {}) {
    commit(RESET_CURRENT_SESSION_CONTENT, currentData);
  },
  //设置historyUpdateFlag 值
  resetHistoryUpdateFlag({ commit }, flag = false) {
    commit(RESET_HISTORY_UPDATE_FLAG, flag);
  },
  //重新设置群组列表
  resetGroupList({ commit }, dataList = []) {
    commit(RESET_GROUP_LIST, dataList);
  },
  //设置视频语音通话数据
  setCurrentVoiceVideoSessionContent({ commit }, sessionData = null) {
    commit(SET_CURRENT_VOICEVIDEO_SESSION_CONTENT, sessionData);
  },
  //清空语音视频通话数据
  clearCurrentVoiceVideoSessionContent({ commit }) {
    commit(CLEAR_CURRENT_VOICEVIDEO_SESSION_CONTENT);
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
